package com.retail.shop.service.impl;


import cn.hutool.core.util.ObjectUtil;
import com.retail.core.entity.result.Result;
import com.retail.shop.entity.vo.RetailAdminShopVo;
import com.retail.core.entity.ParamReq;
import com.retail.shop.entity.request.RetailAdminShopRequest;
import com.retail.shop.entity.vo.RetailShopVo;
import com.retail.shop.enums.ExamineVerifyEnums;
import com.retail.shop.mapper.RetailAdminShopMapper;
import com.retail.shop.mapper.RetailShopMapper;
import com.retail.shop.service.RetailAdminShopService;
import com.retail.system.entity.vo.AdminVo;
import com.retail.system.mapper.AdminMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

/**
 * 用户管理单位 Service impl
 *
 * @author Administrator
 */
@Service
public class RetailAdminShopServiceImpl implements RetailAdminShopService {

    @Resource
    private RetailAdminShopMapper retailAdminShopMapper;

    @Resource
    private AdminMapper adminMapper;

    @Resource
    private RetailShopMapper retailShopMapper;

    @Override
    public RetailAdminShopVo getModel(Long id) {
        RetailAdminShopVo retailAdminShopVo = retailAdminShopMapper.getModel(id);
        retailAdminShopVo.setExamineVerifyName(ExamineVerifyEnums.getStatusValues(retailAdminShopVo.getExamineVerify()));

        setAdminName(retailAdminShopVo);
        setShopName(retailAdminShopVo);

        return retailAdminShopVo;
    }

    private void setShopName(RetailAdminShopVo retailAdminShopVo) {
        RetailShopVo shopVo = retailShopMapper.getModel(retailAdminShopVo.getShopId());
        if (ObjectUtil.isNotEmpty(shopVo)) {
            retailAdminShopVo.setShopName(shopVo.getPoiName());
        }
    }

    private void setAdminName(RetailAdminShopVo retailAdminShopVo) {
        Long adminId = retailAdminShopVo.getAdminId();
        AdminVo adminVo = adminMapper.getModel(adminId);
        if (ObjectUtil.isNotEmpty(adminVo)) {
            retailAdminShopVo.setAdminName(adminVo.getUsername());
        }
    }

    @Transactional
    @Override
    public Long save(RetailAdminShopRequest retailAdminShopRequest) {
        retailAdminShopMapper.save(retailAdminShopRequest);
        return retailAdminShopRequest.getId();
    }

    @Transactional
    @Override
    public Long update(RetailAdminShopRequest retailAdminShopRequest) {
        retailAdminShopMapper.update(retailAdminShopRequest);
        return retailAdminShopRequest.getId();
    }

    @Override
    public List<RetailAdminShopVo> listPage(ParamReq<RetailAdminShopRequest> params) {
        params.setStartIndex((params.getCurrent() - 1) * params.getSize());
        List<RetailAdminShopVo> ret = retailAdminShopMapper.listPage(params);
        ret.forEach(r -> {
            r.setExamineVerifyName(ExamineVerifyEnums.getStatusValues(r.getExamineVerify()));

            setAdminName(r);
            setShopName(r);
        });

        return ret;
    }

    @Override
    public Integer countPage(ParamReq<RetailAdminShopRequest> params) {
        return retailAdminShopMapper.countPage(params);
    }

    @Override
    public Integer updateDelFlagById(Long id, Integer delFlag) {
        return retailAdminShopMapper.updateDelFlagById(id, delFlag);
    }

    @Override
    public Result<Integer> examineVerify(RetailAdminShopRequest request) {
        Integer count = retailAdminShopMapper.examineVerify(request);

        if (ObjectUtil.isNotEmpty(request) && ObjectUtil.equal(request.getExamineVerify(), 1)) {
            adminMapper.updateOwerShop(request.getAdminId());
            Integer auditStatus = 1;    // 审核通过
            List<AdminVo> adminvos = adminMapper.findAllAdminsByAuditStatus(auditStatus);
            adminvos.forEach(admin -> {
                Long adminId = admin.getId();
                Integer groupCount = selectGroupShopCount(adminId, 3);
                adminMapper.updateGroupShop(request.getAdminId(), groupCount);
            });
        }

        return Result.success(count);
    }

    @Override
    public Integer findModelsCountByAdminIdAndUpgradeDays(Long adminId, Long upgradeDay) {
        return retailAdminShopMapper.findModelsCountByAdminIdAndUpgradeDays(adminId, upgradeDay);
    }


    private Integer selectGroupShopCount(Long adminId, Integer deepness) {
        AtomicReference<Integer> root = new AtomicReference<>(0);
        if (deepness > 0) {
            AdminVo adminVo = adminMapper.getModel(adminId);

            if (ObjectUtil.isNotEmpty(adminVo)
                    && ObjectUtil.equal(adminVo.getAuditStatus(), 1)
                    && ObjectUtil.equal(adminVo.getStatusFlag(), 1)
                    && ObjectUtil.equal(adminVo.getDelFlag(), 0)) {
                root.updateAndGet(v -> v + adminVo.getOwnerShopCount());
                List<AdminVo> childAdmins = adminMapper.getChildByParentId(adminVo.getId());
                childAdmins.forEach(child -> {
                    root.updateAndGet(v -> v + selectGroupShopCount(child.getId(), deepness - 1));
                });
            }
        }
        return root.get();
    }
}
